﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Graphics graphics;
        public Pen pen;
        public double per1 { get; set; }
        public double per2 { get; set; }

        public double th1 { get; set; }
        public double th2 { get; set; }

        public int depth { get; set; }

        public int n { get; set; }

        public int length { get; set; }
        void drawCayleyTree(int n, double x0, double y0, double length, double th)
        {
            if (n == 0)
                return;
            double x1 = x0 + length * Math.Cos(th);
            double y1 = y0 + length * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * length, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * length, th - th2);

        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            graphics.DrawLine(pen, (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            drawCayleyTree(n, 300, 300, length, -Math.PI / 2);
        }
    }
}
